package com.love.marriage.oss.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.common.interfaces.avoid.AvoidRepeatableCommit;
import com.love.marriage.common.vo.Result;
import com.love.marriage.oss.service.FileInfoService;
import com.love.marriage.oss.vo.FileInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * description: 文件信息表restApi
 * author: alex
 * createDate: 2023-01-30 14:08:29
 * version: 1.0.0
 */
@ApiSort(20)
@Tag(name = "文件信息表相关接口", description = "文件信息表相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/file-info")
public class FileInfoController {

    private final FileInfoService fileInfoService;

    @ApiOperationSupport(order = 10, author = "alex")
    @Operation(summary = "获取文件信息表分页", description = "获取文件信息表分页")
    @PostMapping(value = "/page")
    @Parameters({
            @Parameter(description = "页码", name = "pageNum", schema = @Schema(type = "Integer", example = "1")),
            @Parameter(description = "每页大小", name = "pageSize", schema = @Schema(type = "Integer", example = "10"))}
    )
    public Result<Page<FileInfoVo>> getPage(@RequestParam(value = "pageNum", required = false) Long pageNum,
                                            @RequestParam(value = "pageSize", required = false) Long pageSize,
                                            @RequestBody(required = false) FileInfoVo fileInfoVo) {
        return Result.success(fileInfoService.getPage(pageNum, pageSize, fileInfoVo));
    }

    @ApiOperationSupport(order = 20, author = "alex")
    @Operation(summary = "获取文件信息表详情", description = "获取文件信息表详情")
    @GetMapping
    public Result<FileInfoVo> query(@RequestParam(value = "id") Long id) throws Exception {
        return Result.success(fileInfoService.queryFileInfo(id));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增文件信息表", description = "新增文件信息表")
    @PostMapping
    @Tag(name = "公共", description = "公共接口")
    public Result<FileInfoVo> add(@RequestPart(value = "file") MultipartFile file) throws Exception {
        return Result.success(fileInfoService.addFileInfo("common", file));
    }

    @AvoidRepeatableCommit
    @ApiOperationSupport(order = 30, author = "alex")
    @Operation(summary = "新增文件信息类型", description = "新增文件信息类型")
    @PostMapping(value = "fileType")
    public Result<FileInfoVo> add(@RequestParam(value = "type") String type,
                                  @RequestPart(value = "file") MultipartFile file) throws Exception {
        return Result.success(fileInfoService.addFileInfo(type, file));
    }

    @ApiOperationSupport(order = 40, author = "alex")
    @Operation(summary = "修改文件信息表", description = "修改文件信息表")
    @PutMapping
    public Result<FileInfoVo> update(@RequestParam(value = "id") Long id,
                                     @RequestParam(value = "type", required = false) String type,
                                     @RequestPart(value = "file") MultipartFile file) throws Exception {
        return Result.success(fileInfoService.updateFileInfo(id, type, file));
    }

    @ApiOperationSupport(order = 50, author = "alex")
    @Operation(summary = "刪除文件信息表", description = "刪除文件信息表")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.success(fileInfoService.deleteFileInfo(ids));
    }

    @ApiOperationSupport(order = 60, author = "alex")
    @Operation(summary = "文件下载", description = "文件下载")
    @GetMapping("/fileDownload")
    @Parameters({
            @Parameter(description = "id", name = "id", required = true, schema = @Schema(type = "Long", example = "10"))}
    )
    public Result<InputStream> fileDownload(@RequestParam(value = "id") Long id) {
        return Result.success(fileInfoService.fileDownload(id));
    }

    @ApiOperationSupport(order = 70, author = "alex")
    @Operation(summary = "获取文件信息", description = "获取文件信息")
    @GetMapping("/getFileInfo")
    @Parameters({
            @Parameter(description = "文件id列表", name = "fileIdList", required = true, schema = @Schema(type = "List"))}
    )
    public Result<List<FileInfoVo>> getFileInfo(@RequestParam(value = "fileIdList") List<Long> fileIdList) {
        return Result.success(fileInfoService.getFileInfo(fileIdList));
    }
}
